<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="书刊编码" prop="bookCode">
        <el-input
          v-model.trim="queryParams.bookCode"
          style="width: 200px"
          placeholder="请输入书刊编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书刊名称" prop="orderName">
        <el-input
          v-model.trim="queryParams.orderName"
          style="width: 200px"
          placeholder="请输入书刊名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版编号" prop="pubNo">
        <el-input
          v-model.trim="queryParams.pubNo"
          style="width: 200px"
          placeholder="请输入出版编号"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="在职状态" prop="joinPositionStatus">
                <el-select style="width: 200px;" v-model="queryParams.joinPositionStatus" placeholder="请选择在职状态">
                    <el-option v-for="item in mes_user_position_status" :key="item.value" :value="item.value"
                        :label="item.label">
                    </el-option>
                </el-select>
            </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          v-hasPermi="['production:order:add']"
          @click="handleAdd"
        >
          新增
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
                <el-button type="primary" plain icon="Upload" @click="handleImport">导入订单信息</el-button>
            </el-col> -->
      <el-col :span="1.5">
        <!-- <el-button type="primary" plain icon="Download" @click="handleExport">导出</el-button> -->
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>
    <div>
      <el-table v-loading="loading" border :data="dataList" height="65vh">
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          label="状态"
          min-width="100"
          align="center"
          prop="status"
        >
          <template v-slot="{ row }">
            <!-- <dict-tag :options="ems_order_status" :value="row.status" /> -->
            <el-tag :type="row.status == 0 ? 'danger' : 'success'">
              {{ row.status == 0 ? "作废" : "正常" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="书刊编码"
          min-width="160"
          align="center"
          prop="bookCode"
        >
          <template v-slot="{ row }">
            <el-button type="primary" @click="handleView(row)" text>
              {{ row.bookCode }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="出版编号"
          align="center"
          min-width="100"
          prop="pubNo"
        ></el-table-column>
        <el-table-column
          label="书刊名称"
          align="center"
          min-width="240"
          prop="orderName"
        ></el-table-column>
        <el-table-column
          label="科目"
          align="center"
          min-width="100"
          prop="subjectType"
        >
          <template v-slot="{ row }">
            <dict-tag
              :options="ems_workshop_subject_type"
              :value="row.subjectType"
            />
          </template>
        </el-table-column>
        <!-- <el-table-column label="印刷数量(册)" width="120" align="center" :show-overflow-tooltip="true"
                    prop="printingCount" v-if="columns[4].visible">
                </el-table-column> -->
        <el-table-column
          label="书号"
          min-width="120"
          align="center"
          prop="bookNo"
        ></el-table-column>
        <!-- <el-table-column
                    label="定价"
                    min-width="120"
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="fixPrice"
                    v-if="columns[5].visible"
                >
                </el-table-column> -->
        <el-table-column
          label="出版社"
          min-width="100"
          align="center"
          prop="publishingHouse"
        >
        </el-table-column>
        <el-table-column
          label="作废原因"
          min-width="100"
          align="center"
          prop="voidReason"
        ></el-table-column>
        <!-- <el-table-column label="打包方式" width="150" align="center" :show-overflow-tooltip="true" prop="packWay"
                    v-if="columns[8].visible">
                    <template v-slot="{ row }">
                        <dict-tag :options="ems_order_pack_way" :value="row.packWay" />
                    </template>
                </el-table-column>
                <el-table-column label="文件日期" min-width="100" align="center" :show-overflow-tooltip="true" prop="fileDate"
                    v-if="columns[9].visible">
                    <template v-slot="{ row }">
                        {{row.fileDate? row.fileDate.split('T')[0]:null }}
                    </template>
                </el-table-column> -->
        <el-table-column
          label="备注"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="remark"
        ></el-table-column>
        <el-table-column
          label="创建人"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="createBy"
        ></el-table-column>
        <el-table-column
          label="创建日期"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="createTime"
        >
          <template v-slot="{ row }">
            {{ row.createTime.split("T")[0] }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="100"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-tooltip
              content="修改"
              v-hasPermi="['production:order:edit']"
              placement="top"
            >
              <el-button
                v-if="scope.row.status != 0"
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              content="删除"
              v-hasPermi="['production:order:del']"
              placement="top"
            >
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              content="复制新增"
              v-hasPermi="['production:order:copy']"
              placement="top"
            >
              <el-button
                v-if="scope.row.status == 0"
                link
                type="primary"
                icon="CopyDocument"
                @click="handleCopy(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-button
              v-if="scope.row.status != 0"
              link
              type="primary"
              @click="handleInvalidate(scope.row)"
            >
              作废
            </el-button>
            <!-- <el-button
                            link
                            type="primary"
                            v-hasPermi="['production:order:submit']"
                            v-if="scope.row.status == 1"
                            @click="handleSubmit(scope.row)"
                            >提交</el-button
                        > -->
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 用户导入对话框 -->
    <el-dialog
      :title="upload.title"
      v-model="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="uploadRef"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <el-icon class="el-icon--upload"><upload-filled /></el-icon>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <template #tip>
          <div class="el-upload__tip text-center">
            <div class="el-upload__tip">
              <el-checkbox v-model="upload.updateSupport" />
              是否更新已经存在的用户数据
            </div>
            <span>仅允许导入xls、xlsx格式文件。</span>
            <el-link
              type="primary"
              :underline="false"
              style="font-size: 12px; vertical-align: baseline"
              @click="importTemplate"
            >
              下载模板
            </el-link>
          </div>
        </template>
      </el-upload>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import { downLoad } from "@/utils/ruoyi";
import {
  getMesOrderInfo,
  delMesOrderInfo,
  editMesOrderInfo,
  exportMesOrder,
  updateStatus,
} from "@/api/production/order";
import { ElMessage, ElMessageBox } from "element-plus";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const total = ref(0);
const router = useRouter();
import { getToken } from "@/utils/auth";
import { onActivated } from "@vue/runtime-core";
/*** 用户导入参数 */
const upload = reactive({
  // 是否显示弹出层（用户导入）
  open: false,
  // 弹出层标题（用户导入）
  title: "",
  // 是否禁用上传
  isUploading: false,
  // 是否更新已经存在的用户数据
  updateSupport: 0,
  // 设置上传的请求头部
  headers: { Authorization: "Bearer " + getToken() },
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "/mes/user/importMesUserInfo",
});
// 列显隐信息
const columns = ref([
  { key: 0, label: `状态`, visible: true },
  { key: 1, label: `订单编码`, visible: true },
  { key: 2, label: `书刊名称`, visible: true },
  { key: 3, label: `科目`, visible: true },
  { key: 4, label: `书号`, visible: true },
  { key: 5, label: `定价`, visible: true },
  { key: 6, label: `出版社`, visible: true },
  { key: 7, label: `备注`, visible: true },
  { key: 8, label: `创建人`, visible: true },
  { key: 9, label: `创建日期`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 50,
    orderName: undefined,
    orderNo: undefined,
  },
});
const { queryParams } = toRefs(data);
const { ems_order_pack_way, ems_workshop_subject_type, ems_order_status } =
  proxy.useDict(
    "ems_order_status",
    "ems_order_pack_way",
    "ems_workshop_subject_type"
  );

onActivated(() => {
  getList();
});
const getList = () => {
  loading.value = true;
  getMesOrderInfo(queryParams.value)
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};
/// 作废
const handleInvalidate = (row) => {
  ElMessageBox.prompt("请输入作废原因", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    inputPattern: /^.+$/,
    inputErrorMessage: "请输入作废原因",
  })
    .then(({ value }) => {
      updateStatus({ pid: row.pid, voidReason: value, status: 0 }).then(
        (res) => {
          getList();
          proxy.$modal.msgSuccess("作废成功");
        }
      );
    })
    .catch(() => {});
};
// 新增
const handleAdd = () => {
  router.push("/production/order/orderDetails");
};
// 编辑
const handleUpdate = (row) => {
  router.push({
    path: "/production/order/orderDetails",
    query: {
      id: row.pid,
    },
  });
};
// 查看
const handleView = (row) => {
  router.push({
    path: "/production/order/orderDetails",
    query: {
      id: row.pid,
      info: true,
    },
  });
};
// 复制新增
const handleCopy = (row) => {
  router.push({
    path: "/production/order/orderDetails",
    query: {
      id: row.pid,
      copy: true,
    },
  });
};

// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除"' + row.orderNo + '"的订单？')
    .then(function () {
      return delMesOrderInfo({ pid: row.pid });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
// 提交
const handleSubmit = (row) => {
  proxy.$modal
    .confirm('是否确认提交"' + row.orderNo + '"的订单？')
    .then(function () {
      return editMesOrderInfo({ status: 2, pid: row.pid });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("提交成功");
    })
    .catch(() => {});
};
// 导出
const handleExport = () => {
  exportMesOrder(queryParams.value).then((res) => {
    downLoad(res.msg, "订单列表");
  });
};
/** 导入按钮操作 */
function handleImport() {
  upload.title = "员工导入";
  upload.open = true;
}
/** 下载模板操作 */
function importTemplate() {
  exportTemplate().then((res) => {
    downLoad(res.msg, "人员模版");
  });
}
/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
  upload.isUploading = true;
};
/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
  upload.open = false;
  upload.isUploading = false;
  proxy.$refs["uploadRef"].handleRemove(file);
  proxy.$alert(
    "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
      response.msg +
      "</div>",
    "导入结果",
    { dangerouslyUseHTMLString: true }
  );
  getList();
};

/** 提交上传文件 */
function submitFileForm() {
  proxy.$refs["uploadRef"].submit();
}
getList();
// router.beforeEach((to, from, next) => {
//     if (to.path == "/basicInfo/personnel") {
//         getList()
//     }
//     next();
// });
</script>

<style lang="scss" scoped></style>
